package com.morehair.config;

import com.morehair.config.realm.AdminRealm;
import com.morehair.config.realm.WxRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * Author  kkw
 * Date  2021/6/5 15:46
 * Version 1.0
 * Description :
 */
@Configuration
public class ShiroConfig {

    //shiro组件注册
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
//        shiroFilterFactoryBean.setLoginUrl("/wx/auth/login");//认证失败重定向
        shiroFilterFactoryBean.setLoginUrl("/admin/auth/index");//认证失败重定向
        //前台的时候,未认证也会重定向????????????????
//        未经授权重定向
//        shiroFilterFactoryBean.setUnauthorizedUrl("/admin/auth/unAuth");
        //对请求的url进行过滤
//        shiroFilterFactoryBean.lo
        LinkedHashMap<String, String> filterMap = new LinkedHashMap<>();
        //anon匿名拦截器,对这些不拦截

        filterMap.put("/admin/auth/index","anon");
        filterMap.put("/admin/auth/unAuth","anon");
        filterMap.put("/druid/**","anon");
        filterMap.put("/pic/**","anon");
        filterMap.put("/wx/auth/login","anon");
//        filterMap.put("/wx/goods/detail","anon");

        filterMap.put("/wx/auth/regCaptcha","anon");
        filterMap.put("/wx/auth/register","anon");
        filterMap.put("/wx/auth/reset","anon");
        //类别
        filterMap.put("/wx/catalog/index","anon");
        filterMap.put("/wx/catalog/current","anon");
        //查找
        filterMap.put("/wx/search/**","anon");
        //品牌
        filterMap.put("/wx/brand/**","anon");
        filterMap.put("/wx/cart/goodscount","anon");


        // 优惠券
        filterMap.put("/wx/coupon/list","anon");

        // 专题
        filterMap.put("/wx/topic/list","anon");
        filterMap.put("/wx/topic/detail","anon");
        filterMap.put("/wx/topic/related","anon");

        //商品详情
//        filterMap.put("/wx/goods/details","anon");
//        filterMap.put("/wx/goods/**","anon");
        filterMap.put("/wx/home/index","anon");
        filterMap.put("/wx/goods/count","anon");


        filterMap.put("/admin/auth/login","anon");


//        filterMap.put("/wx/home/index","anon");
//        filterMap.put("/unAuthc","anon");
        filterMap.put("/**","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

        return shiroFilterFactoryBean;
    }

    @Bean
    //注册securityManager
    public DefaultWebSecurityManager securityManager(AdminRealm adminRealm,DefaultWebSessionManager webSessionManager,
                                                     CustomAuthenticator authenticator) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(adminRealm);
        //defaultWebSecurityManager.setRealm(wxRealm);
        //-------------------------------
        defaultWebSecurityManager.setAuthenticator(authenticator);
        defaultWebSecurityManager.setSessionManager(webSessionManager);
        return defaultWebSecurityManager;
    }

    //声明式鉴权,注解需要的组件
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    @Bean
    public DefaultWebSessionManager webSessionManager() {
        CustomSessionManger customSessionManger = new CustomSessionManger();
//        customSessionManger.setGlobalSessionTimeout(3600000);
        //单位是毫秒
        return customSessionManger;
    }

    @Bean
    public CustomAuthenticator authenticator(AdminRealm adminRealm, WxRealm wxRealm) {
        CustomAuthenticator customAuthenticator = new CustomAuthenticator();
        ArrayList<Realm> realms = new ArrayList<>();
        realms.add(adminRealm);
        realms.add(wxRealm);
        customAuthenticator.setRealms(realms);
        return customAuthenticator;
    }

}
